package main

import (
	"context"

	"github.com/asim/go-micro/v3/client"
	"github.com/asim/go-micro/v3/registry"
	"github.com/go-micro/plugins/v3/registry/etcd"
	"smeb.xyz/pub/websocket-subandpub-cluster/internal/handlerwebsocket/protos"
)

func InitGrpcClient(urls string) client.Client {
	client := client.NewClient(
		client.Registry(
			etcd.NewRegistry(
				registry.Addrs(urls),
			),
		),
	)
	return client
}

func GetGrpcServiceByServerName(servername string) protos.GrpcService {
	if GrpcService[servername] == nil {
		GrpcService[servername] = protos.NewGrpcService(servername, GrpcClient)
	}
	return GrpcService[servername]
}

func RpcPublishRpcMsg(servername string, msg *protos.PublishToChannelRequest) (bool, error) {

	// request the Hello method on the Greeter handler
	_, err := GetGrpcServiceByServerName(servername).PublishToChannel(context.TODO(), msg)

	if err != nil {
		ZapLogger.Error("PublishRpcMsg error: ", err)
		return false, err
	}

	return true, nil
}

func RpcOnChannelDeleted(servername string, msg *protos.DeleteChannelRequest) (bool, error) {

	// request the Hello method on the Greeter handler
	_, err := GetGrpcServiceByServerName(servername).DeleteChannel(context.TODO(), msg)

	if err != nil {
		ZapLogger.Error("OnChannelDeleted error: ", err)
		return false, err
	}

	return true, nil
}

func RpcDeleteChannelMembers(servername string, msg *protos.DeleteChannelMembersRequest) (bool, error) {

	// request the Hello method on the Greeter handler
	_, err := GetGrpcServiceByServerName(servername).DeleteChannelMembers(context.TODO(), msg)

	if err != nil {
		ZapLogger.Error("DeleteChannelMembers error: ", err)
		return false, err
	}

	return true, nil
}

// func RpcUserSubscribleChannels(servername string, msg *protos.Use) (bool, error) {

// 	// request the Hello method on the Greeter handler
// 	b, err := GetGrpcServiceByServerName(servername).UserSubscribleChannels(context.TODO(), msg)

// 	if !b.IsSuccess {
// 		ZapLogger.Error("UserSubscribleChannels error: ", err)
// 	}

// 	return b.IsSuccess, err
// }

func RpcUserSomeoneMessage(servername string, msg *protos.PublishToSomeoneInChannelRequest) (bool, error) {

	// request the Hello method on the Greeter handler
	_, err := GetGrpcServiceByServerName(servername).PublishToSomeoneInChannel(context.TODO(), msg)

	if err != nil {
		ZapLogger.Error("UserE2EMessage error: ", err)
		return false, err
	}

	return true, nil
}

func RpcUserSomebodyMessage(servername string, msg *protos.PublishToSomebodyInChannelRequest) (bool, error) {

	// request the Hello method on the Greeter handler
	_, err := GetGrpcServiceByServerName(servername).PublishToSomebodyInChannel(context.TODO(), msg)

	if err != nil {
		ZapLogger.Error("UserE2EMessage error: ", err)
		return false, err
	}

	return true, nil
}

func RpcMultiUserSubscribleChannel(servername string, msg *protos.MultipleIdentitySubscribleChannelRequest) (bool, error) {

	// request the Hello method on the Greeter handler
	_, err := GetGrpcServiceByServerName(servername).MultipleIdentitySubscribleChannel(context.TODO(), msg)

	if err != nil {
		ZapLogger.Error("MultiUserSubscribleChannel error: ", err)
		return false, err
	}

	return true, nil
}

func SendKickedMsg(servername string, msg *protos.KickedOutRequest) (bool, error) {

	// request the Hello method on the Greeter handler
	b, err := GetGrpcServiceByServerName(servername).SendKickedOutMsg(context.TODO(), msg)

	if err != nil {
		ZapLogger.Error("SendKickedMsg error: ", err)
	}
	return b.IsSuccess, err
}

func GetServerInfo(servername string, msg *protos.GetServerInfoRequest) (*protos.GetServerInfoResponse, error) {

	// request the Hello method on the Greeter handler
	b, err := GetGrpcServiceByServerName(servername).GetServerInfo(context.TODO(), msg)

	if err != nil {
		ZapLogger.Error("SendKickedMsg error: ", err)
		return nil, err
	}
	return b, nil
}
